<?php

namespace app\common\product\model;


use app\common\config\logic\DictionaryLogic;
use app\common\config\model\ConfigCompany;
use app\common\product\logic\StoreCategoryLogic;
use app\common\product\logic\StoreSupplierLogic;
use think\Model;
use think\model\concern\SoftDelete;

class StoreProduce extends Model
{
    protected $table = "tk_store_produce";
    
    use SoftDelete;
    protected $defaultSoftDelete = 0;

    protected $type = [
        'titlepic' => 'json',
    ];

    public function getCompanyAttr($value, $data)
    {
        if (empty($data['company_id'])) {
            return [
                "id" => "",
                "title" => ""
            ];
        }

        $logic = new DictionaryLogic();
        return $logic->getItem($data['company_id'], ConfigCompany::class, ['id', 'title', 'short_name']);
    }

    public function getCategoryAttr($value, $data)
    {
        $logic = new StoreCategoryLogic();
        return $logic->getStoreCategory($data['category_id'], ['id', 'title']);
    }

    public function getSupplierAttr($value, $data)
    {
        if (empty($data['supplier_id'])) {
            return [
                "id" => "",
                "title" => ""
            ];
        }

        $logic = new StoreSupplierLogic();
        return $logic->getStoreSupplier($data['supplier_id'], ['id', 'title']);
    }
}
